
<div class="layui-form">
    <div class="layui-inline" style="padding: 4px">
        <label class="layui-form-label">商品</label>
        <div class="layui-input-inline">
            <select id="kmlist_shop" lay-filter="kmlist_shop" lay-search="">
                <option value="">不筛选商品</option>
            </select>
        </div>
    </div>

    <div class="layui-inline" style="padding: 4px">
        <label class="layui-form-label">状态</label>
        <div class="layui-input-inline">
            <select id="kmlist_state" lay-filter="kmlist_state" lay-verify="required">
                <option value="">不筛选状态</option>
                <option value="0">已售</option>
                <option value="1">待售</option>
            </select>
        </div>
    </div>


</div>

<table id="demo" lay-filter="test"></table>

<script type="text/html" id="barDemo">
    <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a>
</script>
<script type="text/html" id="toolbarDemo">
    <div class="layui-btn-container">
        <button class="layui-btn layui-btn-sm" onclick="plsc()">批量删除</button>
    </div>
</script>

<script>
    var myTable,shops;

    function formatDate(now) {
        now = new Date(now*1000);
        return now.getFullYear()
            + "-" + (now.getMonth()>8?(now.getMonth()+1):"0"+(now.getMonth()+1))
            + "-" + (now.getDate()>9?now.getDate():"0"+now.getDate())
            + " " + (now.getHours()>9?now.getHours():"0"+now.getHours())
            + ":" + (now.getMinutes()>9?now.getMinutes():"0"+now.getMinutes())
            + ":" + (now.getSeconds()>9?now.getSeconds():"0"+now.getSeconds());

    }
    var type;
    layui.use(['form','table','laydate'], function(){
        var table = layui.table,form = layui.form;
        $.post("admin/getShop","iscard=1",function (data) {
            for (var i = 0;i<data.data.length;i++){
                $("#kmlist_shop").append('<option value="'+data.data[i].id+'">'+data.data[i].shopname+'</option>');
            }
            shops=data.data;

            //第一个实例
            myTable = table.render({
                elem: '#demo'
                ,height: 'full-160'
                ,url: 'admin/getKm'
                ,cellMinWidth:100
                ,toolbar: '#toolbarDemo'
                ,defaultToolbar:[]
                ,cols: [[ //表头
                    //{field: 'id', title: '编号', sort: true},
                    {type:'checkbox'},
                    {field: 'content', title: '卡密内容'},
                    {field: 'shopid', title: '商品名',templet: function(d){
                            for (var i = 0;i<shops.length;i++){
                                if (shops[i].id==d.shopid){
                                    return shops[i].shopname;
                                }
                            }
                            return "";
                        }
                    },
                    {field: 'state', title: '状态',sort: true,templet: function(d){
                            if (d.state==0){
                                return "已售";
                            }
                            return "待售";
                        }
                    },
                    {field: 'updatetime', title: '上传时间',templet: function(d){

                            return formatDate(d.updatetime);
                        }
                    },
                    {field: 'selltime', title: '售出时间',templet: function(d){
                            if (d.selltime==0){
                                return "待售";
                            }
                            return formatDate(d.updatetime);
                        }
                    },
                    {field: 'orderid', title: '所属订单',templet: function(d){
                            if (d.orderid==-1){
                                return "待售";
                            }
                            return d.orderid;
                        }
                    },

                    {title: '操作', align:'center', toolbar: '#barDemo'}
                ]]
                ,page:true
            });
            layui.form.render();

        });


        

        //监听行工具事件
        table.on('tool(test)', function(obj){
            var data = obj.data //获得当前行数据
                ,layEvent = obj.event;

            if(layEvent === 'del'){
                layer.confirm('确定执行操作吗？', function(index){

                    layer.close(index);
                    //向服务端发送删除指令
                    $.post("admin/delKm","id="+data.id+"&shopid="+data.shopid,function (data) {
                        if (data.code==1){
                            obj.del(); //删除对应行（tr）的DOM结构
                        }

                        layer.msg(data.msg);
                    });
                });
            }
        });

        form.on('select(kmlist_shop)', function(data){
            console.log(data.value); //得到被选中的值
            myTable.reload({
                where: {
                    shopid:$("#kmlist_shop").val(),
                    state:$("#kmlist_state").val()
                } ,
                page: {
                    curr: 1 //重新从第 1 页开始
                }
            });
        });
        form.on('select(kmlist_state)', function(data){
            console.log(data.value); //得到被选中的值
            myTable.reload({
                where: {
                    shopid:$("#kmlist_shop").val(),
                    state:$("#kmlist_state").val()
                } ,
                page: {
                    curr: 1 //重新从第 1 页开始
                }
            });
        });


        table.on('edit(test)', function(obj){
            var value = obj.value //得到修改后的值
                ,data = obj.data //得到所在行所有键值
                ,field = obj.field; //得到字段
            $.post("admin/editShop","id="+data.id+"&key="+field+"&value="+value,function (data) {
                layer.msg(data.msg);
            });
        });

        form.render();

    });


    function plsc() {
        layer.confirm('确定执行操作吗？', function(index){

            layer.close(index);

            dodel(layui.table.checkStatus('demo').data,0);
        });
    }
    function dodel(data,index) {
        if (data.length<=index){
            layer.msg("操作成功!");
            myTable.reload({
                where: {
                    shopid:$("#kmlist_shop").val(),
                    state:$("#kmlist_state").val()
                } ,
                page: {
                    curr: 1 //重新从第 1 页开始
                }
            });
            return;
        }
        //向服务端发送删除指令
        $.post("admin/delKm","id="+data[index].id+"&shopid="+data[index].shopid,function (res) {
            if (res.code==-1){
                layer.msg(res.msg);
                return;
            }
            dodel(data,index+1);

        });
    }
</script>
